package com.opengit;

import com.sun.imageio.plugins.bmp.BMPImageReaderSpi;
import com.sun.imageio.plugins.gif.GIFImageReaderSpi;
import com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi;
import com.sun.imageio.plugins.png.PNGImageReaderSpi;
import com.sun.imageio.plugins.wbmp.WBMPImageReaderSpi;
import com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReaderSpi;
import com.twelvemonkeys.imageio.plugins.tiff.TIFFImageWriterSpi;

import javax.imageio.*;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.spi.ImageWriterSpi;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class TIFFExports {

    public BufferedImage getBufferedImage(String filePath) {
        ImageReaderSpi spi = null;
        BufferedImage bufferedImage = null;

        try {
            File fInputFile = new File(filePath);
            InputStream fis = new BufferedInputStream(new FileInputStream(fInputFile));

            String ext = filePath.substring(filePath.lastIndexOf(".") + 1).toLowerCase();

            switch (ext) {
                case "bmp":
                    spi = new BMPImageReaderSpi();
                    break;
                case "gif":
                    spi = new GIFImageReaderSpi();
                    break;
                case "jpg":
                case "jpeg":
                    spi = new JPEGImageReaderSpi();
                    break;
                case "png":
                    spi = new PNGImageReaderSpi();
                    break;
                case "tif":
                    spi = new TIFFImageReaderSpi();
                    break;
                case "wbmp":
                    spi = new WBMPImageReaderSpi();
                    break;
                default:
                    break;
            }
            if (spi != null) {
                ImageReader reader = spi.createReaderInstance();
                ImageInputStream iis = ImageIO.createImageInputStream(fis);
                reader.setInput(iis, true);
                bufferedImage = reader.read(reader.getMinIndex());
            } else {
                bufferedImage = null;
            }
        } catch (Exception e) {
            bufferedImage = null;
        }
        return bufferedImage;
    }

    public void OneToOne(String filePath, BufferedImage bufferedImage) {
        File file = new File(filePath);

        List<BufferedImage> bufferedImages = new ArrayList<BufferedImage>();
        bufferedImages.add(bufferedImage);

        MultipleToOne(file, bufferedImages);
    }

    public void OneToOne(File fOutputFile, BufferedImage bufferedImage) {
        List<BufferedImage> bufferedImages = new ArrayList<BufferedImage>();
        bufferedImages.add(bufferedImage);

        MultipleToOne(fOutputFile, bufferedImages);
    }

    /**
     * 多个@BufferedImage文件合并为一个TIFF文件
     *
     * @param filePath:       输出文件路径
     * @param bufferedImages: @BufferedImage列表
     */
    public void MultipleToOne(String filePath, List<BufferedImage> bufferedImages) {
        File file = new File(filePath);
        MultipleToOne(file, bufferedImages);
    }


    public void MultipleToOne(File fOutputFile, List<BufferedImage> bufferedImages) {
        ImageWriterSpi imageWriterSpi = null;
        ImageWriter writer = null;
        ImageWriteParam param = null;
        ImageOutputStream ios = null;

        try {
            ImageIO.scanForPlugins();

            imageWriterSpi = new TIFFImageWriterSpi();
            writer = imageWriterSpi.createWriterInstance();

            param = writer.getDefaultWriteParam();
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            param.setCompressionType("LZW");
            param.setCompressionQuality(0.75f);

            ios = ImageIO.createImageOutputStream(fOutputFile);

            writer.setOutput(ios);
            writer.prepareWriteSequence(null);

            int imageSize = bufferedImages.size();
            for (int i = 0; i < imageSize; i++) {
                writer.writeToSequence(new IIOImage(bufferedImages.get(i), null, null), param);
            }
            writer.endWriteSequence();
        } catch (Exception e) {

        } finally {
            if (writer != null) {
                writer.dispose();
            }
            if (ios != null) {
                try {
                    ios.close();
                } catch (IOException e) {
                    // e.printStackTrace();
                }
            }
        }
    }
}
